package com.ruoyi.merchant.service.impl;

import com.ruoyi.common.core.cg.pojo.Image;
import com.ruoyi.merchant.mapper.PngMapper;
import com.ruoyi.merchant.service.PngService;
import com.ruoyi.merchant.service.SynthesisService;
import com.ruoyi.merchant.utils.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
* @author ？？？
* @description 针对表【png】的数据库操作Service实现
* @createDate 2025-03-04 14:04:13
*/
@Service
@Slf4j
public class PngServiceImpl implements PngService {

    @Autowired
    private PngMapper pngMapper;

    @Autowired
    private SynthesisService synthesisService;

    @Override
    public Image pngSave(MultipartFile frontIdCard, MultipartFile sideIdCard, MultipartFile license, MultipartFile foodHygieneApproval,Integer uid) throws Exception {

        // 检查文件是否为空
        if (frontIdCard == null || frontIdCard.isEmpty()) {
            throw new IllegalArgumentException("前面身份证文件未上传");
        }
        if (sideIdCard == null || sideIdCard.isEmpty()) {
            throw new IllegalArgumentException("反面身份证文件未上传");
        }
        if (license == null || license.isEmpty()) {
            throw new IllegalArgumentException("营业执照文件未上传");
        }
        if (foodHygieneApproval == null || foodHygieneApproval.isEmpty()) {
            throw new IllegalArgumentException("食品卫生许可文件未上传");
        }

        // 上传文件并获取 URL
        String frontIdCardUrl = uploadToQiniu(frontIdCard);
        String sideIdCardUrl = uploadToQiniu(sideIdCard);
        String licenseUrl = uploadToQiniu(license);
        String foodHygieneApprovalUrl = uploadToQiniu(foodHygieneApproval);

        // 创建 Png 对象并设置文件 URL
        Image png = new Image();
        png.setFrontIdCard(frontIdCardUrl);
        png.setSideIdCard(sideIdCardUrl);
        png.setLicense(licenseUrl);
        png.setFoodHygieneApproval(foodHygieneApprovalUrl);
        png.setUid(uid);
        // 保存 Png 到数据库
        int saveResult = pngMapper.pngSave(png);

        // 组装并返回 Png 对象
        return png;
    }


        // 身份证识别并保存到用户表
//        byte[] frontIdCardBytes = frontIdCard.getBytes();
//        Map<String, String> idCardInfo = Sample.recognizeIdCard(frontIdCardBytes);
//        String name = idCardInfo.get("name");
//        String idNumber = idCardInfo.get("idNumber");
//
//        // 创建 User 对象并保存到用户表
//        Synthesis synthesis = new Synthesis();
//        synthesis.setUserName(name);
//        synthesis.setInvitationCode(idNumber);
//        synthesisService.SynthesSave(synthesis);

        // 组装并返回 Result
//        return png;
//    }

    // 上传文件到七牛云并返回文件的 URL
    private String uploadToQiniu(MultipartFile file) throws Exception {
//        if (file == null || file.isEmpty()) {
//            log.error("上传文件为空: " + file);
//            throw new IllegalArgumentException("上传的文件不能为空");
//        }

        log.info("开始上传文件: " + file.getOriginalFilename());

        byte[] fileBytes = file.getBytes();
        String fileName = file.getOriginalFilename();
        String fileKey = QiniuUtils.upload2Qiniu(fileBytes, fileName);

        // 去除文件 URL 的协议部分（http:// 或 https://）
        if (fileKey.startsWith("http://") || fileKey.startsWith("https://")) {
            fileKey = fileKey.substring(fileKey.indexOf("/", fileKey.indexOf("://") + 3));
        }

        String fileUrl = "http://stcn3inqu.hb-bkt.clouddn.com" + fileKey;
        log.info("上传文件成功, URL: " + fileUrl);

        return fileUrl;
    }
}

